/*
 * Alexey Danchuk
 * This software is an implementation of the third task from tasks.pdf
 * A little later this code will support russian.
 * For testing I used these popular anagrams from:
 * http://en.wikipedia.org/wiki/Anagram
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

/**
 * The program should receive input of two strings and give the output to screen
 * telling whether these words are anagrams
 * 
 * @version 1.01 27 January 2013
 * @author 	Alexey Danchuk
 */
public class Anagram {

	/**
	 * Method for input two strings.
	 * 
	 * @return strings - inputed strings
	 * @throws Exception
	 */
	public static String[] input() throws Exception {
		BufferedReader inputStream = new BufferedReader(new InputStreamReader(
															System.in));
		/* We need just two strings to compare. */
		String[] strings = new String[2];
		System.out.println("Enter strings, please.");
		for (int i = 0; i < strings.length; i++) {
			System.out.println("String " + (i + 1) + ": ");
			strings[i] = inputStream.readLine();
		}
		inputStream.close();
		return strings;
	}

	/**
	 * Method for anagrams validation.
	 * 
	 * @param inputedStrings - previously entered strings
	 * @return true - are anagrams, false - not
	 */
	public static boolean areAnagram(String[] inputedStrings) {
		String firstStr = inputedStrings[0];
		String secondStr = inputedStrings[1];
		/* Removes all whitespaces and non visible characters such as tab, \n. */
		String firstAnagram = firstStr.toLowerCase().replaceAll("\\s", "");
		String secondAnagram = secondStr.toLowerCase().replaceAll("\\s", "");
		int[] firstConvertedAnagram = convert(firstAnagram);
		int[] secondConvertedAnagram = convert(secondAnagram);
		if (firstConvertedAnagram.length != secondConvertedAnagram.length) {
			return false;
		} else {
			Arrays.sort(firstConvertedAnagram);
			Arrays.sort(secondConvertedAnagram);
			return Arrays.equals(firstConvertedAnagram, secondConvertedAnagram);
		}
	}

	/**
	 * Method for converting a string to the array of character (Unicode code
	 * point).
	 * 
	 * @param string - string to convert
	 * @return codes - unicode code point array
	 */
	public static int[] convert(String string) {
		int[] codes = new int[string.length()];
		for (int i = 0; i < string.length(); i++) {
			codes[i] = string.codePointAt(i);
		}
		return codes;
	}

	/**
	 * Method for output the result of analyzed strings.
	 * 
	 * @param strings - for analysis
	 */
	public static void output(String[] strings) {
		System.out.println("Strings are anagrams? " + "Answer is: "
				+ areAnagram(strings));
	}

	/**
	 * Main method. Entry point into the program.
	 * 
	 * @param args - command-Line arguments, this allows the user to specify
	 *               configuration information when the application is launched.
	 */
	public static void main(String[] args) {
		try {
			String[] strings = input();
			output(strings);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
